package com.czk.algorithm.kmp.practice;

/**
 * 暴力KMP匹配法
 *
 * @Author:ChenZhangKun
 * @Date: 2020/8/7 22:48
 */
public class ViolenceMatch {
    public static void main(String[] args) {
        String str1 = "guishdjshds";
        String str2 = "dj";
        int i = violenceMatch(str1, str2);
        System.out.println(i);
    }

    public static int violenceMatch(String str1, String str2) {
        // 转换为字符数组
        char[] char1 = str1.toCharArray();
        char[] char2 = str2.toCharArray();
        int i=0;
        int j=0;
        while (i<char1.length&&j < char2.length) {
            if (char1[i]==char2[j]){
                i++;
                j++;
            }else {
                i=i-j+1;
                j=0;
            }
            if (j==char2.length){
                return i-j;
            }
        }
        return -1;
    }
}
